package com.hoshiicloud.goods.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hoshiicloud.common.query.OmnipotentParam;
import com.hoshiicloud.common.rpc.Response;
import com.hoshiicloud.common.utils.BeanUtils;
import com.hoshiicloud.goods.entity.spu.SpuSalesCategory;
import com.hoshiicloud.goods.param.spu.SpuSalesCategoryAddParam;
import com.hoshiicloud.goods.param.spu.SpuSalesCategoryQueryParam;
import com.hoshiicloud.goods.param.spu.SpuSalesCategoryUpdateParam;
import com.hoshiicloud.goods.service.SpuSalesCategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 营销类目 控制器
 * </p>
 *
 * @author zhangshuhu
 * @since 2019-06-04
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/goods/spu-sales-category")
@Api(value = "商品管理", tags = {"营销类目管理"})
public class SpuSalesCategoryController {
    @Autowired
    private SpuSalesCategoryService spuSalesCategoryService;

    /**
     * @param param
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 分页查询营销类目
     */
    @ApiOperation(value = "分页查询营销类目")
    @PostMapping("/query/page")
    public Response page(@RequestBody @Valid SpuSalesCategoryQueryParam param) {
        try {
            log.info("分页查询营销类目");
            QueryWrapper<SpuSalesCategory> queryWrapper = new QueryWrapper<SpuSalesCategory>();
            queryWrapper.lambda().eq(param.getSalesCategoryId() != null, SpuSalesCategory::getSalesCategoryId, param.getSalesCategoryId())
                    .eq(param.getExtSpuId() != null, SpuSalesCategory::getExtSpuId, param.getExtSpuId())
                    .eq(param.getDeleted() != null, SpuSalesCategory::getDeleted, param.getDeleted());
            IPage<SpuSalesCategory> page = new Page<>(Long.parseLong(param.getPageNumber()), Long.parseLong(param.getPageSize()));
            String key = "SpuSalesCategoryQueryParam{"+param.toString() + "},Page{pageNumber=" + param.getPageNumber() + ",pageSize=" + param.getPageSize() + "}";
            
            return Response.data(spuSalesCategoryService.getPage(page, queryWrapper, key));
        } catch (Exception e) {
            log.error("分页查询营销类目异常", e);
            return Response.failed("分页查询营销类目异常");
        }
    }

    /**
     * @param id
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 根据id查询营销类目
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询营销类目")
    @ApiParam(name = "id", value = "主键ID", required = true)
    public Response queryById(@PathVariable("id") Long id) {
        return Response.data(spuSalesCategoryService.getById(id));
    }

    /**
     * @param param
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 新增
     */
    @PostMapping("/save")
    @ApiOperation(value = "新增营销类目")
    public Response save(@RequestBody @Valid SpuSalesCategoryAddParam param) {
        // Bean数据转换
        SpuSalesCategory entity;
        try {
            entity = BeanUtils.covertType(param, SpuSalesCategory.class);
        } catch (Exception e) {
            log.error("Bean转换异常", e);
            return Response.failed("Bean转换异常");
        }
        return Response.data(spuSalesCategoryService.save(entity));
    }

    /**
     * @param param
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 修改营销类目
     */
    @PutMapping("/update")
    @ApiOperation(value = "修改营销类目")
    public Response update(@RequestBody @Valid SpuSalesCategoryUpdateParam param) {
        // Bean数据转换
        SpuSalesCategory entity;
        try {
            entity = BeanUtils.covertType(param, SpuSalesCategory.class);
        } catch (Exception e) {
            log.error("Bean转换异常", e);
            return Response.failed("Bean转换异常");
        }
        return Response.data(spuSalesCategoryService.updateById(entity));
    }

    /**
     * @param id
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 删除营销类目
     */
    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "删除营销类目")
    @ApiParam(name = "id", value = "主键ID", required = true)
    public Response delete(@PathVariable("id") Long id) {
        return Response.data(spuSalesCategoryService.deleteById(id));
    }

    /**
     * @Author:campchen
     * @Description: StringUtils.join(ids, " ") 为缓存中添加唯一KEY  如不合适可选择更好方法
     * @Date:上午 10:18 2019/5/11
     * @Param: ids
     * @return: com.hoshiicloud.common.rpc.Response
     */
    @ApiOperation(value = "根据id集合查询营销类目")
    @GetMapping("/getByIds")
    public Response getByIds(@RequestParam List<Long> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return Response.failed("id不能为空");
        }
        ids.sort(null);
        return Response.data(spuSalesCategoryService.getByIds(ids, StringUtils.join(ids, "")));
    }
    @ApiOperation(value = "根据自定义条件查询营销类目")
    @PostMapping("/getByParam")
    public Response getByParam(@RequestBody SpuSalesCategoryQueryParam param) {
        QueryWrapper<SpuSalesCategory> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(param.getSalesCategoryId() != null, SpuSalesCategory::getSalesCategoryId, param.getSalesCategoryId())
                .eq(param.getExtSpuId() != null, SpuSalesCategory::getExtSpuId, param.getExtSpuId())
                .eq(param.getDeleted() != null, SpuSalesCategory::getDeleted, param.getDeleted());
        return Response.data(spuSalesCategoryService.getByParam(queryWrapper));
    }

    @ApiOperation(value = "根据ext_spu_ids 获取营销类目")
    @GetMapping("/listBySpuIds")
    public Response<List<SpuSalesCategory>> listBySpuIds(@RequestParam("spuIds") List<Long> spuIds){
        return Response.data(spuSalesCategoryService.listBySpuIds(spuIds));
    }

    @ApiOperation(value = "批量保存")
    @PostMapping("/saveBatch")
    public Response<Boolean> saveBatch(@RequestBody List<SpuSalesCategory> list){
        if(CollectionUtils.isEmpty(list)){
            return Response.data(true);
        }
        return Response.data(spuSalesCategoryService.saveBatch1(list));
    }

    @ApiOperation(value = "批量更新")
    @PostMapping("/updateBatch")
    public Response<Boolean> updateBatch(@RequestBody OmnipotentParam<SpuSalesCategory> param) {
        return Response.data(spuSalesCategoryService.updateBatch(param));
    }

}
